﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.WorkREST3();
            Console.ReadLine();
        }

        public Program()
        {
            //ThreadStart t = new ThreadStart(WorkREST3);
            //Thread thread = new Thread(t);
            //thread.Start();
        }

        public void WorkSOAP()
        {
            using (ReservationServiceReference.ReservationServiceClient client = new ReservationServiceReference.ReservationServiceClient())
            {
                client.Open();
                IList<ReservationServiceReference.BatteryType> types = client.GetAllBatteryTypes();
                foreach (var type in types)
                {
                    Console.WriteLine("Average charging time => " + type.average_charging_time + " of type with id => " + type.id);
                }
            }
            Console.ReadLine();
        }

        public async void WorkREST()
        {
            using(HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                using(HttpResponseMessage response = 
                    await client.GetAsync("http://localhost:8733/Design_Time_Addresses/WcfServiceLayer/Service1/GetAllAccounts"))
                {
                    if(response.IsSuccessStatusCode)
                    {
                        string jsonResponse = await response.Content.ReadAsStringAsync();
                        //dynamic data = JObject.Parse(jsonResponse);
                        Console.WriteLine(jsonResponse);
                    }
                    else
                    {
                        Console.WriteLine("Service didn't work");
                    }
                }
            }
        }

        public async void WorkREST2()
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                using (HttpResponseMessage response =
                    await client.PostAsJsonAsync("http://localhost:8733/Design_Time_Addresses/WcfServiceLayer/Service1/CheckBatteryType",
                    new BatteryType { id = 43 } ))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        Console.WriteLine("Service worked");
                        string bulshit = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(bulshit);
                    }
                    else
                    {
                        Console.WriteLine("Service didn't work");
                    }
                }
            }
        }

        public async void WorkREST3()
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //client.Timeout = new TimeSpan(1000 * 1000 * 15);
                JsonThingie thingie = new JsonThingie()
                {
                    endCoords = new double[] { 55.6761, 12.5683 },
                    startCoords = new double[] { 57.0500, 9.9167 },
                    userAccount = new UserAccount() { id = 2, session_key = "kfeo32323434324234" },
                    desiredType = new BatteryType() { id = 1 }
                };
                Debug.WriteLine(JsonConvert.SerializeObject(thingie));

                using (HttpResponseMessage response =
                    await client.PostAsJsonAsync("http://localhost:8733/Design_Time_Addresses/WcfServiceLayer/Service1/Graph", thingie))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        string jsonResponse = await response.Content.ReadAsStringAsync();
                        //dynamic data = JObject.Parse(jsonResponse);
                        Debug.WriteLine(jsonResponse);
                    }
                    else
                    {

                        Console.WriteLine(response.StatusCode);
                        Console.WriteLine("Service didn't work");
                    }
                }
                Console.WriteLine("After response");
            }
        }

        public class BatteryType
        {
            public int id { get; set; }
 
        }


        public class UserAccount
        {
            public int id { get; set; }
            public string session_key { get; set; }
        }

        public class JsonThingie
        {
            public double[] startCoords { get; set; }
            public double[] endCoords { get; set; }
            public BatteryType desiredType { get; set; }
            public UserAccount userAccount { get; set; }
        }
    }
}
